import { ref, reactive } from 'vue';
import { deposit } from '@/server/deposit';
import { toast } from '@/service/status';

export const depositPage = reactive({
    is_show: false,
    open() {
        depositPage.is_show = true;
    },
    close() {
        depositPage.is_show = false;
        depositPage.resetData();
    },
    resetData() {
        depositPage.amount = 0;
        depositPage.fileObj.file = null;
        depositPage.fileObj.base64 = '';
    },
    // 充值金额
    amount: 0,
    // 实时更新的文件对象
    fileObj: {
        file: null,
        base64: '',
    }
});

export const withdrawPage = reactive({
    is_show: false,
    open() {
        withdrawPage.is_show = true;
    },
    close() {
        withdrawPage.is_show = false;
        withdrawPage.resetData();
    },
    resetData() {
        withdrawPage.amount = 0;
    },
    // 提现金额
    amount: 0,
});

export const depositList = ref([
    // {
    //     id: "",
    //     amount: "0.00",
    //     channel: 1,
    //     image: "",
    //     status: 0,
    //     createTime: 0,
    // }
]);
export const withdrawList = ref([
    // {
    //     id: "",
    //     amount: "0.00",
    //     finalAmount: "0.00",
    //     handFee: "0.00",
    //     serviceFee: "0.00",
    //     receiptInfo: "",
    //     status: 0,
    //     createTime: 0,
    // }
]);

export const flowList = ref([
    // {
    //     id 流水id
    //     amount 金额
    //     beforeBalance 变动前余额
    //     beforeFreeze 变动前冻结
    //     afterBalance 变动后余额
    //     afterFreeze 变动后冻结
    //     detail 详情
    //     createTime 创建时间
    // }
]);

export const depositService = {
    async deposit() {
        // 接收参数
        const amount = depositPage.amount;
        const file = depositPage.fileObj.file;
        // 验证参数
        if (!amount || amount <= 0) alert('amount min 1');
        if (!file) alert('not file');
        // 构造 FormData
        const formData = new FormData();
        formData.append('amount', amount);
        formData.append('voucher', file);
        // 调用 API
        let res = await deposit.deposit(formData);
        if (res.success) {
            if (res.data.code == 200) {
                toast.success('Deposit success!');
                depositPage.resetData();
            } else {
                toast.warning('Deposit failed!');
            }
        } else {
            if (res.err.message == 'token is null') {
                toast.warning('Deposit Failed! Please try again after login!', 2000);
            } else {
                toast.warning('Deposit failed!');
            }
        }
    },
    async withdraw() {
        // 接收参数
        let amount = withdrawPage.amount;
        // 验证参数
        if (!amount || amount <= 0) alert('amount min 1');
        amount = String(parseFloat(amount).toFixed(2));
        // 调用 API
        let res = await deposit.withdraw(amount);
        if (res.success) {
            if (res.data.code == 200) {
                toast.success('Withdraw success!');
                withdrawPage.resetData();
            } else {
                toast.warning('Withdraw failed!');
            }
        } else {
            if (res.err.message == 'token is null') {
                toast.warning('Withdraw Failed! Please try again after login!', 2000);
            } else {
                toast.warning('Withdraw failed!');
            }
        }
    },
    async getDepositList() {
        let res = await deposit.getDepositList();
        if (res.success) {
            if (res.data.code == 200) {
                depositList.value = res.data.data.list;
            } else {
                console.log('获取充值列表失败 需要给用户一定提示才行', res);
            }
        } else {
            console.log('获取充值列表失败 需要给用户一定提示才行', res);
        }
    },
    async getWithdrawList() {
        let res = await deposit.getWithdrawList();
        if (res.success) {
            if (res.data.code == 200) {
                withdrawList.value = res.data.data.list;
            } else {
                console.log('获取充值列表失败 需要给用户一定提示才行', res);
            }
        } else {
            console.log('获取充值列表失败 需要给用户一定提示才行', res);
        }
    },
    async getFlowList() {
        let res = await deposit.getFlowList();
        if (res.success) {
            if (res.data.code == 200) {
                flowList.value = res.data.data.list;
            } else {
                console.log('获取流水记录失败 需要给用户一定提示才行', res);
            }
        } else {
            console.log('获取流水记录失败 需要给用户一定提示才行', res);
        }
    }
};